{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "source": [
    "# 多个独立样本比较的秩和检验\n",
    "\n",
    "## 案例\n",
    "\n",
    "通过不同途径给予霍乱弧菌疫苗，研究 21 天后各组的血清抗体滴度水平之间是否存在差异。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (96, 3)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>ID</th><th>ab titer</th><th>group</th></tr><tr><td>i64</td><td>i64</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>1</td><td>&quot;G1-80&quot;</td></tr><tr><td>2</td><td>1</td><td>&quot;G1-80&quot;</td></tr><tr><td>3</td><td>1</td><td>&quot;G1-100&quot;</td></tr><tr><td>4</td><td>1</td><td>&quot;G1-100&quot;</td></tr><tr><td>5</td><td>1</td><td>&quot;G1-100&quot;</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>92</td><td>16</td><td>&quot;G2&quot;</td></tr><tr><td>93</td><td>16</td><td>&quot;G2&quot;</td></tr><tr><td>94</td><td>16</td><td>&quot;G2&quot;</td></tr><tr><td>95</td><td>16</td><td>&quot;G2&quot;</td></tr><tr><td>96</td><td>32</td><td>&quot;G2&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (96, 3)\n",
       "┌─────┬──────────┬────────┐\n",
       "│ ID  ┆ ab titer ┆ group  │\n",
       "│ --- ┆ ---      ┆ ---    │\n",
       "│ i64 ┆ i64      ┆ str    │\n",
       "╞═════╪══════════╪════════╡\n",
       "│ 1   ┆ 1        ┆ G1-80  │\n",
       "│ 2   ┆ 1        ┆ G1-80  │\n",
       "│ 3   ┆ 1        ┆ G1-100 │\n",
       "│ 4   ┆ 1        ┆ G1-100 │\n",
       "│ 5   ┆ 1        ┆ G1-100 │\n",
       "│ …   ┆ …        ┆ …      │\n",
       "│ 92  ┆ 16       ┆ G2     │\n",
       "│ 93  ┆ 16       ┆ G2     │\n",
       "│ 94  ┆ 16       ┆ G2     │\n",
       "│ 95  ┆ 16       ┆ G2     │\n",
       "│ 96  ┆ 32       ┆ G2     │\n",
       "└─────┴──────────┴────────┘"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import polars as pl\n",
    "\n",
    "df_raw = pl.read_csv(\"B_10_6-data.csv\")\n",
    "\n",
    "lf_raw = df_raw.lazy()\n",
    "lf_raw.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAjIAAAGZCAYAAACT/9vpAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/GU6VOAAAACXBIWXMAAA9hAAAPYQGoP6dpAAApfUlEQVR4nO3df1RVdb7/8deRHwcVOAgoqGGCmD/QrLCMIb3+QIHG1Ml1x7JZ+WvqdkXTUHPRlKZO0rLS8hcuv5nWKkenmaymGSnHrjJj+AslmUZJUkcdPThXBRQVEM/3j5bndgY0jsI556PPx1p7LfZ7/3ofTi5e7f3Ze1scDodDAAAABmrm7QYAAABuFEEGAAAYiyADAACMRZABAADGIsgAAABjEWQAAICxCDIAAMBYBBkAAGAsf2830NSuXLmiEydOKCQkRBaLxdvtAACABnA4HDp37pzatWunZs2ufd7llg8yJ06cUExMjLfbAAAAN+DYsWO64447rrn8lg8yISEhkr7/RYSGhnq5GwAA0BAVFRWKiYlx/h2/lls+yFy9nBQaGkqQAQDAMD82LITBvgAAwFgEGQAAYCyCDAAAMNYtP0YGAIB/V1tbq5qaGm+3cVsLCAiQn5/fTe+HIAMAuG04HA7Z7XaVlZV5uxVICgsLU3R09E09540gAwC4bVwNMW3atFGLFi14UKqXOBwOXbhwQadOnZIktW3b9ob3RZABANwWamtrnSEmIiLC2+3c9po3by5JOnXqlNq0aXPDl5kY7AsAuC1cHRPTokULL3eCq65+FzczXokgAwC4rXA5yXc0xndBkAEAAMYiyAAAAGMRZAAAgLEIMgAA+Kjq6mpvt+DzCDIAAHjIuXPn9MQTT6hly5Zq27atFi1apP79+2vq1KmSpI4dO2revHl68sknFRoaqqefflqS9Pvf/14JCQmyWq3q2LGj3njjDZf9WiwWffzxxy61sLAwrVmzRpJ05MgRWSwWrVu3Tj/5yU8UFBSkHj16aOvWrU39kZscQQYAAA/JzMzUtm3b9Omnn2rTpk36y1/+oj179ris8/rrr6tXr17au3evXnrpJRUUFOjnP/+5HnvsMRUVFenll1/WSy+95Awp7pgxY4amTZumvXv3KikpSY888ohOnz7dSJ/u5iQvSXaZGooH4gEA4AHnzp3Tu+++q7Vr12rQoEGSpNWrV6tdu3Yu6w0cOFDTpk1zzj/xxBMaNGiQXnrpJUnSXXfdpb///e967bXXNHbsWLd6mDRpkkaOHClJysnJUW5urlatWqXnn3/+Jj6Zd3FGBgAADzh06JBqamr0wAMPOGs2m01dunRxWa93794u8/v371dysusZiuTkZB08eFC1tbVu9ZCUlOT82d/fX71799b+/fvd2oevIcgAAOBDWrZs6fY2FotFDofDpXa7vN2bIAMAgAfExcUpICBAu3btctbKy8v17bffXne7bt26adu2bS61bdu26a677nK+n6h169Y6efKkc/nBgwd14cKFOvvavn278+fLly+roKBA3bp1u6HP4ysYIwMAgAeEhIRozJgxmjFjhsLDw9WmTRvNnj1bzZo1u+6j+qdNm6b7779f8+bN06hRo5Sfn6+lS5dq+fLlznUGDhyopUuXKikpSbW1tZo5c6YCAgLq7GvZsmXq3LmzunXrpkWLFuns2bMaP358k3xeT+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q5cueLdRgF4FEEGgJHS0tJ08uRJHTlyRBs3btSAAQM0ZcoUDR06VJcv132AF4Bbk8+PkQGA+litVudjGNq3b6/77rtPDz74oAYNGqQ1a9bol7/8pZc7BOAJnJEBcMsYOHCgevXqpY8++sjbrQDwEIIMgFtK165ddeTIEW+3AcBDCDIAbikOh4PXkQC3EYIMgFvK/v37FRsb6+02AHgIQQbALePLL79UUVGRRo4c6e1WAHgIdy0BMFJVVZXsdrtqa2tVWlqq3NxcZWdna+jQoXryySe93R4ADyHIADBSbm6u2rZtK39/f7Vq1Uq9evXS4sWLNWbMGDVrxslm4HbBKwoAAICx+N8WAABgLIIMAAAwFkEGAAAYiyADAACMRZABAADGIsgAAABjEWQAAICxCDIAAMBYBBkAAGAsggwAADAWQQYAABiLIAMAAIz1/wFe/M6oUxvCHQAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "sns.barplot(data=lf_raw.collect(), x=\"ID\", y=\"ab titer\", hue=\"group\")\n",
    "\n",
    "# 隐藏 x 轴刻度标签\n",
    "plt.xticks([])\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "影响变量为多项无序分类变量，结果变量为多项有序分类变量。采用 Kruskal-Wallis 秩和检验推断各组样本的总体分布有无差别。\n",
    "\n",
    "### 假设\n",
    "\n",
    "$ H_0 $: 三组血清抗体滴度水平的总体分布相同  \n",
    "$ H_1 $: 三组血清抗体滴度水平的总体分布不全相同\n",
    "\n",
    "### 假设检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Statistic: 12.267219147642413\n",
      "P-value: 0.0021687385933763757\n",
      "Significance: True\n"
     ]
    }
   ],
   "source": [
    "from scipy.stats import kruskal\n",
    "\n",
    "def filter_by_group(lf: pl.LazyFrame) -> list[pl.LazyFrame]:\n",
    "    groups = lf.select(pl.col(\"group\")).unique().collect().to_series()\n",
    "\n",
    "    return [lf.filter(pl.col(\"group\") == group) for group in groups]\n",
    "\n",
    "res = kruskal(*map(lambda x: x.select(\"ab titer\").collect(), filter_by_group(lf_raw)))\n",
    "# test\n",
    "#res = kruskal(lf_raw.filter(pl.col(\"group\") == \"G1-80\").select(\"ab titer\").collect(), lf_raw.filter(pl.col(\"group\") == \"G1-100\").select(\"ab titer\").collect())\n",
    "\n",
    "print(f\"\"\"Statistic: {float(res.statistic.sum())}\n",
    "P-value: {float(res.pvalue.sum())}\n",
    "Significance: {res.pvalue.sum() < 0.05}\"\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 结果分析\n",
    "\n",
    "P < 0.05, 在 $ \\alpha = 0.05 $ 的检验水准下，拒绝 $ H_0 $，接受 $ H_1 $，三组血清抗体滴度水平具有统计学意义。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
